<?php
/**
 * Description of Emms_Cron_Arrears
 * @author andres
 * 
 * Updates arrears table
 * One new record reflecting several stats is created every day for each 
 * delinquent loan. Depending on the delinquent portfolio size, the update 
 * method could take quite some time, be sure to adjust the php run overtime 
 * accordingly.
 *
 */
require_once APPLICATION_PATH . '/modules/loan/models/Loans.php';
require_once APPLICATION_PATH . '/modules/loan/models/LoanStatusChanges.php';
require_once APPLICATION_PATH . '/modules/loan/models/Payments.php';
require_once APPLICATION_PATH . '/modules/loan/models/LoanWriteOffCharges.php';
require_once APPLICATION_PATH . '/modules/loan/models/LoanWriteOffPayments.php';

class Emms_Cron_Arrears extends Emms_Cron implements Emms_Interface_Cron
{
    private $_arrears;
    private $_dloans;
    
    public function __construct() 
    {
        parent::__construct();
        
        try {
            $activeLoans = new Zend_Db_Table(array('name' => 'active_loans', 'primary' => 'LoanId'));
            $this->_dloans = $activeLoans->fetchAll($activeLoans->select()->where('CurrentDelay > 0')); //->limit(10)
            $this->_arrears = new Zend_Db_Table(array('name' => 'arrears', 'primary' => 'ArrearId'));
        } catch  (Zend_Exception $e) {
            throw $e;
        }
    }
    
    public function update()
    {
        $this->db->beginTransaction();
        try {
            foreach ($this->_dloans as $row) {
                
                $plan = new Emms_PmtPlan(new Emms_Loan($row->LoanId));
                
                $payment = 0;
                $penalty = 0;
                $interest = 0;
                $loanInsurance = 0;
                $healthInsurance = 0;
                $administrativeFee = 0;
                $principal = 0;
                $totalDelay = 0;
                $hits = 0;
                
                foreach($plan->getSchPmtObj() as $schPayment) {
                    
                    if($schPayment->Paid) continue;
                    if($schPayment->Date > $this->date) break;
                    
                    $payment += $schPayment->Pmt;
                    $penalty += $schPayment->Penalties;
                    $interest += $schPayment->Interest;
                    $loanInsurance += $schPayment->LoanInsurance;
                    $healthInsurance += $schPayment->HealthInsurance;
                    $administrativeFee += $schPayment->AdministrativeFee;
                    $principal += $schPayment->Principal;
                    $totalDelay += $schPayment->Delay;
                    $hits++;
                }
        
                $this->_arrears->insert(array(
                    'LoanId' => $row->LoanId,
                    'Date' => $this->date,
                    'BalancePrincipal' => $row->BalancePrincipal,
                    'BalanceLoanInsurance' => $row->BalanceLoanInsurance,
                    'BalanceHealthInsurance' => $row->BalanceHealthInsurance,
                    'BalanceAdministrativeFee' => $row->BalanceAdministrativeFee,
                    'Payment' => $payment,
                    'Penalty' => $penalty,
                    'Interest' => $interest,
                    'LoanInsurance' => $loanInsurance,
                    'HealthInsurance' => $healthInsurance,
                    'AdministrativeFee' => $administrativeFee,
                    'Principal' => $principal,
                    'Hits' => $hits,
                    'Delay' => $row->CurrentDelay,
                    'TotalDelay' => $totalDelay
                ));
            }
        } catch  (Zend_Exception $e) {
            
            $this->db->rollBack();
            throw $e;
        }
        $this->db->commit();
    }
}




